package com.jiazhong.basic.shuxin.thread;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.FutureTask;

public class App {

    private static void a() {
        // 新建状态：产生了线程类或者其子类的对象时
        DemoA a1 = new DemoA();
        DemoA a2 = new DemoA();
        a1.setPriority(2);
        a2.setPriority(8);
        // 就绪状态：当调用start方法时，并不一定会执行
        a1.start();
        a2.start();
    }

    private static void b() {
        // 仅仅是一个任务
        DemoB b1 = new DemoB();
        DemoB b2 = new DemoB();
        // 产生线程类的对象
        Thread t1 = new Thread(b1);
        Thread t2 = new Thread(b2);
        t1.start();
        t2.start();
    }

    @SneakyThrows
    private static void c() {
        DemoC c1 = new DemoC();
        DemoC c2 = new DemoC();
        // 任务
        FutureTask<String> ft1 = new FutureTask<>(c1);
        FutureTask<String> ft2 = new FutureTask<>(c2);
        // 线程
        Thread t1 = new Thread(ft1);
        Thread t2 = new Thread(ft2);
        t1.start();
        t2.start();
        // 获取到返回结果
        String result1 = ft1.get();
        String result2 = ft2.get();
        System.out.println(result1);
        System.out.println(result2);
    }


    private static void d1() {
        DemoD d = new DemoD();
        DemoD1 d1 = new DemoD1(d);
        DemoD1 d2 = new DemoD1(d);
        new Thread(d1).start();
        new Thread(d2).start();
    }

    private static void d2() {
        DemoD2 d = new DemoD2();
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                d.a();
            }
        };
        Runnable r2 = d::a;
        new Thread(r1).start();
        new Thread(r2).start();
    }

    private static void d3() {
        DemoD3 d = new DemoD3();
        Runnable r1 = d::d3;
        Runnable r2 = d::d3;
        new Thread(r1).start();
        new Thread(r2).start();
    }

    private static void d4() {
        DemoD4 d = new DemoD4();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                d.changeNumber();
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                d.changeNumber();
            }
        }).start();

    }

    @SneakyThrows
    private static void d5() {
        List<String> list = new ArrayList<>();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                list.add(i + "");
            }
        }).start();
        new Thread(() -> {
            for (int i = 0; i < 50; i++) {
                list.add(i + "");
            }
        }).start();
        Thread.sleep(3000);
        for (String s : list) {
            System.out.println(s);
        }
    }

    public static void main(String[] args) {
        d5();
    }
}
